import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class IOSUIScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _IOSUIScreenPage();
  }
}

class _IOSUIScreenPage extends State<IOSUIScreen> {
  int position = 0;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return CupertinoTabScaffold(
        tabBar: CupertinoTabBar(items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
              icon: Icon(CupertinoIcons.home), label: 'Tab1'),
          BottomNavigationBarItem(
              icon: Icon(CupertinoIcons.book_solid), label: 'Tab2'),
          BottomNavigationBarItem(
              icon: Icon(CupertinoIcons.tags_solid), label: 'Tab3')
        ]),
        tabBuilder: (context, position) {
          if (position == 0)
            return CupertinoTabView(
              defaultTitle: 'Tabs $position',
              builder: (context) {
//                return IOSUIHomePage();
                return CupertinoPageScaffold(
                  resizeToAvoidBottomInset: true,
                  child: IOSUIHomePage(),
                  navigationBar: CupertinoNavigationBar(),
                );
              },
            );
          else
            return CupertinoTabView(
              defaultTitle: 'Tabb $position',
              builder: (context) {
                return CupertinoPageScaffold(
                  child: Center(
                    child: Text('Tab $position'),
                  ),
                  // 标题由CupertinoTabView.defaultTitle控制，所以这里就不设置标题了
                  navigationBar: CupertinoNavigationBar(),
                );
              },
            );
        });
  }
}

class IOSUIHomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _IOSUIHomePage();
  }
}

class _IOSUIHomePage extends State<IOSUIHomePage> {
  double _slidValue = 0.5;
  bool _switchValue = true;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Padding(
        padding: EdgeInsets.fromLTRB(10, 90, 10, 50),
        child: Table(
          children: <TableRow>[
            TableRow(children: <Widget>[
              CupertinoButton(
                  child: Text('ActionSheet'),
                  onPressed: () {
                    showModalBottomSheet(
                        context: context,
                        builder: (cc) {
                          return CupertinoActionSheet(
                            title: Text('CupertinoActionSheet'),
                            message: Text(
                                'An iOS-style modal bottom action sheet to choose an option among many.'),
                            actions: <Widget>[
                              CupertinoButton(
                                  child: Text('Phone'), onPressed: () {}),
                              CupertinoButton(
                                  child: Text('Compute'), onPressed: () {}),
                              CupertinoButton(
                                  child: Text('Pad'), onPressed: () {})
                            ],
                          );
                        });
                  }),
              CupertinoButton(
                  child: Text('CupertinoAlertDialog'),
                  onPressed: () {
                    // todo 注意有点用cc,有的用context。关闭当前弹窗，使用当前上下文（cc）。
                    // todo 编译出widget, 按理说可以用cc, 但是不行（找不到本地化字符串），这里只能用更上层的context
                    showCupertinoDialog(
                        context: context,
                        builder: (cc) {
                          return CupertinoAlertDialog(
                            title: Text('CupertinoAlertDialog'),
                            content: Text('An iOS-style alert dialog.'),
                            actions: <Widget>[
                              CupertinoDialogAction(
                                child: Text('OK'),
                                isDefaultAction: false,
                                onPressed: () {
                                  Navigator.pop(
                                    cc,
                                  );
                                },
                              ),
                              CupertinoDialogAction(
                                  child: Text('Cancel'),
                                  isDestructiveAction: true,
                                  onPressed: () {
                                    Navigator.pop(
                                      cc,
                                    );
                                  }),
                            ],
                          );
                        });
                  }),
            ]),
            TableRow(children: <Widget>[
              CupertinoButton(
                  child: Text('CupertinoDialog'),
                  onPressed: () {
                    showCupertinoDialog(
                        context: context,
                        builder: (context) {
                          return CupertinoAlertDialog(
                            title: Text("提示"),
                            content: Text(
                              'CupertinoDialog:An iOS-style dialog. 已经废弃。.Use CupertinoPopupSurface for custom popups.',
                              style: Theme.of(context).textTheme.bodyMedium,
                            ),
                          );
                        });
                  }),
              CupertinoButton(
                child: Text(
                  '颜色按钮',
                ),
                onPressed: () {},
                color: CupertinoColors.activeBlue,
                pressedOpacity: 0.7,
              ),
            ]),
            TableRow(children: <Widget>[
              // 菊红控件 和 滑条
              CupertinoActivityIndicator(
                animating: true,
                radius: 20,
              ),
              CupertinoSlider(
                  value: _slidValue,
                  onChanged: (value) {
                    setState(() {
                      _slidValue = value;
                    });
                  }),
            ]),
            TableRow(children: <Widget>[
              // 开关
              CupertinoSwitch(
                  value: _switchValue,
                  onChanged: (b) {
                    setState(() {
                      _switchValue = b;
                    });
                  }),
              CupertinoButton(
                  child: Text('日期选择器'),
                  onPressed: () {
                    Navigator.push(
                        context,
                        CupertinoPageRoute(
                            builder: (context) => CupertinoPageScaffold(
                                  navigationBar: CupertinoNavigationBar(
                                    previousPageTitle: '日期选择器',
                                  ),
                                  child: CupertinoDatePicker(
                                    mode: CupertinoDatePickerMode.date,
                                    onDateTimeChanged: (dateortime) {},
                                    initialDateTime: DateTime.now(),
                                  ),
                                )));
                  }),
            ]),
            TableRow(children: <Widget>[
              CupertinoButton(
                  child: Text('时间选择器'),
                  onPressed: () {
                    Navigator.push(
                        context,
                        CupertinoPageRoute(
                            builder: (context) => CupertinoPageScaffold(
                                  navigationBar: CupertinoNavigationBar(
                                    previousPageTitle: '时间选择器',
                                  ),
                                  child: CupertinoTimerPicker(
                                    onTimerDurationChanged: (time) {},
                                    mode: CupertinoTimerPickerMode.hms,
                                  ),
                                )));
                  }),
              Padding(
                padding: EdgeInsets.all(5),
                child: CupertinoTextField(
                  placeholder: '输入框',
                ),
              ),
            ]),
            TableRow(children: <Widget>[
              CupertinoButton(
                  child: Text('打开新界面'),
                  onPressed: () {
                    Navigator.of(context, rootNavigator: true)
                        .push(CupertinoPageRoute(builder: (cc) {
                      return CupertinoPageScaffold(
                        child: Center(
                            child: Text(
                          '前面一些地方也打开了新界面，但是弹出的界面还在TabScaffold里面。而这个就不一样了，它跳了出来。',
                          style: TextStyle(
                              fontSize: 15,
                              color: CupertinoColors.black,
                              decoration: TextDecoration.none),
                        )),
                        navigationBar: CupertinoNavigationBar(
                          previousPageTitle: '我是新界面',
                        ),
                      );
                    }));
                  }),
              Padding(
                child: CupertinoSegmentedControl(
                  children: {'a': Text('A'), 'b': Text('B')},
                  onValueChanged: (m) {},
                ),
                padding: EdgeInsets.all(10),
              ),
            ]),
            TableRow(children: <Widget>[
              CupertinoButton(
                  child: Text('CupertinoPicker'),
                  onPressed: () {
                    Navigator.push(context,
                        CupertinoPageRoute(builder: (context) {
                      return CupertinoPageScaffold(
                          navigationBar: CupertinoNavigationBar(
                            previousPageTitle: 'CupertinoPicker',
                          ),
                          child: CupertinoPicker.builder(
                            itemExtent: 20,// 两线之间的距离
                            onSelectedItemChanged: (posi) {},
                            itemBuilder: (context, posi) => Text('item $posi'),
                            childCount: 5,
                          ));
                    }));
                  }),
              CupertinoButton(child: Text('a'), onPressed: null),
            ]),
            TableRow(children: <Widget>[
              CupertinoButton(child: Text('a'), onPressed: null),
              CupertinoButton(child: Text('a'), onPressed: null),
            ]),
          ],
          columnWidths: {
            0: FixedColumnWidth(150),
            1: FixedColumnWidth(150),
          },
          border: TableBorder.all(
              color: Colors.red, width: 1, style: BorderStyle.solid),
        ));
  }
}
